<?php
require('../database/db.php');
    error_reporting(0);
	$mysql=new mysql('xzsh');

	// 设置主图
	if ($_REQUEST[num] != '') {
		$num = $_REQUEST[num];
	}else{
		$num = 0;
	}
	// 设置主图 end

	// 设置详情图主图
	if ($_REQUEST[defaultXQTuOrd] != '') {
		$defaultXQTuOrd = $_REQUEST[defaultXQTuOrd];
	}else{
		$defaultXQTuOrd = 0;
	}

	if ($_REQUEST[goodsid] != '') {
		$sql="select * from goods where goodsid=".$_REQUEST[goodsid];
		$goods=$mysql->getRow($sql);
		if ($goods) {
			
			$mode='update';
			$where = ' where goodsid='.$_REQUEST[goodsid];

		}else{
			$mode='insert';
			$where = ' where 1 limit 1';
		}
	}else{
		$mode='insert';
		$where = ' where 1 limit 1';
	}

	$phone=trim($_REQUEST[phone]);
	$name=trim($_REQUEST[name]);
	$shelves=trim($_REQUEST[shelves]);
	$price=trim($_REQUEST[price]);
	$original=trim($_REQUEST[original]);
	$summary=trim($_REQUEST[summary]);
	$can_fanquan=trim($_REQUEST[can_fanquan]);
	$son_class=trim($_REQUEST[son_class]);
	$guige=trim($_REQUEST[guige]);
	$images=trim($_REQUEST[images]);
	$srcimages=trim($_REQUEST[srcimages]);
	$xiangQingTu=trim($_REQUEST[xiangQingTu]);
	$srcXiangQingTu=trim($_REQUEST[srcXiangQingTu]);
	$start_num=trim($_REQUEST[start_num]);
	$Freight_dir=trim($_REQUEST[Freight_dir]);
	$muid=trim($_REQUEST[muid]);
	$guige=explode(';',$guige);
	$length=count($guige);
	$table='goods';

	$shopid = null;
	$shop = $mysql->getRow("select shopid from shop where phone = $phone");
	if($shop){
		$shopid = $shop['shopid'];
	}
	if(!$shopid){
		echo json_encode(array('code'=>-100,'msg'=>'查询不到用户'));
	}
	$arr=array(
		'title'=>$name,
		'shangjia'=>$shelves,
		'price'=>$price,
		'yuanjia'=>$original,
		'summary'=>$summary,
		'can_fanquan'=>$can_fanquan,
		'zixilie'=>$son_class,
		'images'=>$images,
		'srcimages'=>$srcimages,
		'defaultImgOrd'=>$num,
		'xiangQingTu'=>$xiangQingTu,
		'srcXiangQingTu'=>$srcXiangQingTu,
		'defaultXQTuOrd'=>$defaultXQTuOrd,
		'start_num'=>$start_num,
		'Freight_dir'=>$Freight_dir,
		'muid'=>$muid,
		'shopid'=>$shopid
	);
	$result;
	$mysql->autoExecute($table,$arr,$mode,$where);
	if ($mode=='update') {
		$result=$_REQUEST[goodsid];
	}else{
		$result=$mysql->insert_id();
	}

	$sql="DELETE from goodsselect where goodsid=".$result;
	$mysql->query($sql);
	for($i=0;$i<$length;$i++){

		$ar=explode('-',$guige[$i]);
		$tab='goodsselect';
		$ary=array(
			'goodsid'=>$result,
			'fTitle'=>$ar[0],
			'sTitle'=>$ar[1],
			'price'=>$ar[2],
			'number'=>$ar[3]
		);
		$mysql->autoExecute($tab,$ary);
	}
	$result2=$mysql->insert_id();


	if($result!=false && $result2!=false){
		$der=array('code'=>100);
		
	}else{
		$der=array('code'=>200);
	}
	echo json_encode($der);

?>